package com.feituan.core.persistence.service.impl;

import com.alibaba.druid.util.DaemonThreadFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.IntegrationProductMapper;
import com.feituan.core.persistence.mapper.IntegrationRecordMapper;
import com.feituan.core.persistence.service.IIntegrationProductService;
import com.feituan.core.pojo.entity.IntegrationProduct;
import com.feituan.core.pojo.entity.IntegrationRecord;
import com.feituan.core.pojo.request.IntegrationProductReq;
import com.feituan.core.pojo.response.IntegrationProductRes;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IntegrationProductServiceImpl extends ServiceImpl<IntegrationProductMapper, IntegrationProduct> implements IIntegrationProductService {

    @Resource
    private IntegrationRecordMapper integrationRecordMapper;

    /**
     * 新增积分商品
     * @param product  product
     * @return
     */
    @Override
    public int saveProduct(IntegrationProduct product) {
        product.setCreateTime(new Date());
        return this.getBaseMapper().insert(product);
    }


    /**
     * 查询列表
     * @param product product
     * @return
     */
    @Override
    public PageData<IntegrationProductRes> selectList(IntegrationProductReq product) {
        Page<IntegrationProduct> page = new Page<>();
        page.setCurrent(product.getPage());
        page.setSize(product.getLimit());
        LambdaQueryWrapper<IntegrationProduct> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(product.getName()), IntegrationProduct::getName, product.getName());
        Page<IntegrationProduct> productPage = this.baseMapper.selectPage(page, qw);
        if (CollectionUtils.isEmpty(productPage.getRecords())) {
            return PageData.build(1, 0, new ArrayList<IntegrationProductRes>());
        }
        List<IntegrationProductRes> integrationProductRes = new ArrayList<>();

        productPage.getRecords().forEach(el -> {
            IntegrationProductRes integrationProductRes1 = new IntegrationProductRes();
            BeanUtils.copyProperties(el, integrationProductRes1);
            // 查询商品使用数量
            LambdaQueryWrapper<IntegrationRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(true, IntegrationRecord::getProductId, el.getId());
            List<IntegrationRecord> integrationRecords = integrationRecordMapper.selectList(wrapper);
            integrationProductRes1.setUseAmount(integrationRecords.size());
            integrationProductRes1.setSurplusAmount(el.getInventory() - integrationRecords.size());
            integrationProductRes.add(integrationProductRes1);
        });

        PageData<IntegrationProductRes> pageData = new PageData<>();
        pageData.setCurrent(productPage.getCurrent());
        pageData.setTotal(productPage.getTotal());
        pageData.setRecords(integrationProductRes);
        return pageData;
    }

    private void fillInfo() {

    }

    /**
     * 修改商品
     * @param product product
     * @return
     */
    @Override
    public int updateProduct(IntegrationProduct product) {
        product.setUpdateTime(new Date());
        return this.getBaseMapper().updateById(product);
    }

    /**
     * 删除商品
     * @param id id
     * @return
     */
    @Override
    public int deleteProduct(Long id) {
        // 查询商品是否有使用记录
        LambdaQueryWrapper<IntegrationRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, IntegrationRecord::getProductId, id);
        List<IntegrationRecord> integrationRecords = integrationRecordMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(integrationRecords)) {
            return -1;
        }
        return this.getBaseMapper().deleteById(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public IntegrationProductRes selectById(Long id) {
        IntegrationProduct integrationProduct = this.getBaseMapper().selectById(id);
        IntegrationProductRes integrationProductRes = new IntegrationProductRes();
        BeanUtils.copyProperties(integrationProduct, integrationProductRes);
        // 查询商品使用数量
        LambdaQueryWrapper<IntegrationRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, IntegrationRecord::getProductId, id);
        List<IntegrationRecord> integrationRecords = integrationRecordMapper.selectList(wrapper);
        integrationProductRes.setUseAmount(integrationRecords.size());
        integrationProductRes.setSurplusAmount(integrationProduct.getInventory() - integrationRecords.size());
        return integrationProductRes;
    }
}
